package com.atguigu.yygh.hosp.service.impl;

import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {


    @Autowired
    private DepartmentRepository departmentRepository;
    @Override
    public void saveDepartment(Department department) {
        Department departmentDB =departmentRepository.findByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());

        if(departmentDB==null){

            //不存在执行添加

            //为传递的字段
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
        }else {
            //存在,执行修改

            department.setId(departmentDB.getId());
            //为传递的字段
            department.setCreateTime(departmentDB.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(departmentDB.getIsDeleted());
        }

        departmentRepository.save(department);
    }

    @Override
    public Page getDepartment(String hoscode, Integer page, Integer limit) {

        //查询条件
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);

        Sort sort = Sort.by(Sort.Order.asc("depcode"));
        //分页参数
        PageRequest pageRequest = PageRequest.of(page - 1, limit,sort);

        return departmentRepository.findAll(example, pageRequest);
    }

    @Override
    public void removeDepartment(String hoscode, String depcode) {

        Department departmentDB =departmentRepository.findByHoscodeAndDepcode(hoscode,depcode);

        //根据ID删除

        departmentRepository.deleteById(departmentDB.getId());

    }

    @Override
    public List<DepartmentVo> getList(String hoscode) {

        ArrayList<DepartmentVo> result = new ArrayList<>();

        //查询所有的数据
        List<Department> departmentList=departmentRepository.findByHoscode(hoscode);

        //根据bigCode 分组
        Map<String,List<Department>> listMap=departmentList.stream().collect(
                Collectors.groupingBy(Department::getBigcode));

        //遍历map中的数据
        for(Map.Entry<String,List<Department>> entry:listMap.entrySet()){

            //获取相同的bigcode的一组科室
            List<Department> value = entry.getValue();

            //大科室
            DepartmentVo bigDepartment = new DepartmentVo();
            bigDepartment.setDepcode(value.get(0).getBigcode());
            bigDepartment.setDepname(value.get(0).getBigname());

            //提取小科室的名称,编号
            List<DepartmentVo> smallList = new ArrayList<>();
            for (Department department : value) {

                DepartmentVo small = new DepartmentVo();
                small.setDepcode(department.getDepcode());
                small.setDepname(department.getDepname());

                smallList.add(small);

            }
            //在大科室中添加小的科室
            bigDepartment.setChildren(smallList);

            //添加到最终的结果中
            result.add(bigDepartment);



        }
        return result;
    }


}
